"""
title:我的留言
author:陈诚
date:2022/1/7
"""


from Common.Base import Base, open_browser
from Page.LoginPage import LoginPage


# 输入网址
message_url = "http://localhost:8080/ecshop/user.php"


class MyMessage(Base):
    """封装表现层,制作定位器"""
    center_loc = ('link text', '用户中心')                   # 定位用户中心
    mymessage_loc = ('link text', '我的留言')                 # 定位我的留言
    message_loc = ('css selector', 'input[value="0"]')      # 定位留言按钮
    complaints_loc = ('css selector', 'input[value="1"]')   # 定位投诉按钮
    ask_loc = ('css selector', 'input[value="2"]')          # 定位询问按钮
    after_sales_loc = ('css selector', 'input[value="3"]')  # 定位售后按钮
    look_loc = ('css selector', 'input[value="4"]')         # 定位求购按钮
    title_loc = ('name', 'msg_title')                       # 定位主题
    content_loc = ('name', 'msg_content')                   # 定位内容
    file_loc = ('name', 'message_img')                      # 定位上传文件
    button_loc = ('class name', 'bnt_bonus')                 # 定位提交按钮
    result_loc = ('class name', 'f4')                      # 定位返回页面

    """封装操作层,操作元素"""
    def click_center(self):
        """点击用户中心"""
        self.click(self.center_loc)

    def click_mymessage(self):
        """点击我的留言"""
        self.click(self.mymessage_loc)

    def click_message(self):
        """点击留言按钮"""
        self.click(self.message_loc)

    def click_complaints(self):
        """点击投诉按钮"""
        self.click(self.complaints_loc)

    def click_ask(self):
        """点击询问按钮"""
        self.click(self.ask_loc)

    def click_after_sales(self):
        """点击售后按钮"""
        self.click(self.after_sales_loc)

    def click_look(self):
        """点击求购按钮"""
        self.click(self.look_loc)

    def input_title(self, title: str):
        """输入主题"""
        self.send_keys(self.title_loc, title)

    def input_content(self, content: str):
        """输入内容"""
        self.send_keys(self.content_loc, content)

    def input_file(self, file: str):
        """上传文件"""
        self.send_keys(self.file_loc, file)

    def click_button(self):
        """点击确定按钮"""
        self.click(self.button_loc)

    def is_success(self, info):
        """判断留言是否成功"""
        return self.is_text_equal_in_element(self.result_loc, info)


if __name__ == '__main__':
    driver = open_browser()    # 打开浏览器
    login = LoginPage(driver)  # 输入网址
    login.open_url(message_url)
    login.input_username('root1')
    login.input_password(123456)
    login.click_login()

    message = MyMessage(driver)
    message.click_center()
    message.click_mymessage()
    message.click_message()
    message.click_complaints()
    message.click_ask()
    message.click_after_sales()
    message.click_look()
    message.input_title('垃圾产品')
    message.input_content('点都不好用,浪费我的钱钱')
    # message.input_file('E:\ECShop\Data\user_login.xls')
    message.click_button()